Skip to content

fix: repeat and join build in one pass, not quadratically - #271

Merged
jonruttan merged 1 commit into
mainfrom
fix/long-str-memory
Nov 28, 2026
Merged

jonruttan merged 1 commit into
mainfrom
fix/long-str-memory

Conversation

@jonruttan

Copy link
Copy Markdown
Owner

What

In x-python, building a long str or sequence by repetition, or with join, cost memory that grew with the square of the result. On main, s = 'a' * 8000 alone went past the spec guard's 6 GB, while 'a' * 800 peaked at 559 MB. I found this while writing the spec for #270, which had to read data files because it couldn't build long strings.

Cause. A str, bytes or list here is a cons list. Three helpers added each new copy or piece at the end of the result so far, and appending to a cons list copies the whole list:

  • %pb-repeat (lib/python/bytes.x): * for str, bytes and bytearray, and the fill in center/ljust/rjust
  • python-els-repeat (lib/python/runtime-seq.x): * for list and tuple
  • %pb-join: str.join and bytes.join

Fix.

  • The two repeat helpers now put each new copy at the front. That copies only the operand, and because every copy is identical, the order doesn't change.
  • join now pushes each piece and separator onto a reversed accumulator, then reverses it once at the end.

Effect. 'a' * 8000 now peaks at 760 MB, where it was killed past 6 GB before. 'ab' * 50000 peaks at 1042 MB, and an 80,000-character join at 521 MB.

Checking

All runs are on x-lang v0.62.0.

  • New spec 41-string-repeat: one case with expected output recorded from CPython 3.14. It covers str, bytes, bytearray, list and tuple repetition, including zero and negative counts and the element identity that list repetition shares. It also covers str and bytes join and wide padding. It passes, with a peak footprint of 1817 MB. The spec gets its own file because the existing 41-string-build file notes that a string-heavy batch builds up toward the object ceiling.
  • Full suite: 1291 tests, 0 failed.
  • Conformance: 644 tests, 10 failed, down from 12 on main. class_setname_hazard_rand and unicode_ure now pass. The other 10 failures are the same as on main.

Not included

  • s += c in a loop still costs time and memory that grow with the square of the length. With a cons list for the str, each + copies the left side, and fixing that needs a different str representation.
  • str.count: counting through 100,000 characters peaked at 4455 MB. That cost is in the search path, not in building the string.
  • str.zfill doesn't exist in x-python yet.

🤖 Generated with Claude Code

`s * n` for str, bytes and bytearray (%pb-repeat), list and tuple
(python-els-repeat), and str.join and bytes.join (%pb-join) appended each
copy or piece to the END of what was built so far -- and appending to a
list copies it.  So the cost grew with the square of the result:
`'a' * 8000` copied about 32 million pairs and passed the spec guard's
6 GB, where `'a' * 800` took 559 MB.

Each repeat now puts the next copy on the FRONT, which copies only the
copy (every copy is the same, so the order is unchanged), and join pushes
each piece onto a reversed accumulator and turns it round once.
`'a' * 8000` now peaks at 760 MB, and center/ljust/rjust, which fill
through %pb-repeat, are linear with it.

41-string-repeat covers each kind at a size, with output recorded from
CPython 3.14.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jonruttan
jonruttan merged commit 80c94c1 into main Nov 28, 2026
12 checks passed
@jonruttan
jonruttan deleted the fix/long-str-memory branch November 28, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant